package com.lagou.penglei.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.curator.framework.CuratorFramework;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @author ascetic
 * @version 1.0
 * @ClassName DataSourceConfig
 * @Description TODO
 * @date 2020-07-04 15:01
 */
@Configuration
public class DataSourceConfig {

    @Autowired
    private CuratorFramework zkClinet;



    @Bean
    public DataSource dataSource() throws Exception {

        String data = new String(zkClinet.getData().forPath("/dataBase"));
        System.out.printf(data);
        String [] args = data.split("\\|");

        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(args[0].split("=")[1]);
        dataSource.setUrl(args[1].split("=")[1]);
        dataSource.setUsername(args[2].split("=")[1]);
        dataSource.setPassword(args[3].split("=")[1]);
        return dataSource;
    }
}